{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4a40f57f",
   "metadata": {},
   "source": [
    "# Numpy implementation of INT8 matmul"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1b78b202",
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "c5d61794",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import torch\n",
    "from dynamic_quant_ops import quant_matmul, quant_linear"
   ]
  },
  {
   "attachments": {
    "image.png": {
     "image/png": "iVBORw0KGgoAAAANSUhEUgAAApIAAAA0CAYAAAA5Uti8AAAMa2lDQ1BJQ0MgUHJvZmlsZQAASImVVwdYU8kWnluSkJDQQpcSehNEagApIbQA0otgIySBhBJjQlCxo4sKrhURwYquiii2FRA7dmVR7H2xoKKsi7rYUHkTEtB1X/neyTf3/jlz5j8lM7n3AKD5gSuR5KFaAOSLC6QJ4cGMMWnpDNJTgAB1+DEEBlyeTMKKi4sGUAbvf5d3N6A1lKvOCq5/zv9X0eELZDwAkHEQZ/JlvHyIjwOAr+VJpAUAEBV6qykFEgWeA7GuFAYIcbkCZyvxdgXOVOLDAzZJCWyILwOgRuVypdkAaNyDekYhLxvyaHyG2FXMF4kB0BwOcQBPyOVDrIh9eH7+JAWuhNge2ksghvEAZuZ3nNl/488c4udys4ewMq8BUQsRySR53Gn/Z2n+t+TnyQd92MJBFUojEhT5wxreyp0UpcBUiLvFmTGxilpD/EHEV9YdAJQilEckK+1RE56MDesH9CF25XNDoiA2gThMnBcTrdJnZonCOBDD3YJOFRVwkiA2hHihQBaaqLLZKJ2UoPKFNmRJ2SyV/hxXOuBX4euBPDeZpeJ/IxRwVPyYRpEwKRViCsTWhaKUGIg1IHaR5SZGqWxGFQnZMYM2UnmCIn5riBME4vBgJT9WmCUNS1DZl+bLBvPFNgpFnBgV3lcgTIpQ1gc7xeMOxA9zwS4LxKzkQR6B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HXqGjICciiAexBs3bqgBWBdBOJZcV1BkVefmzZuVu7brUmcoysKQDAV1tAkCIBCUAB9YThyXRMuXL9djNwIVkOOO5PihRYsWkRy5IUvrfLBvoOyues7xUTR+/Hi6f/++Gtzi1cAFAiDwfwnwWbZ04MABPX5s8ODBtWqMNwrSxIkT9YggfuFA2BqRojwMyVr9CaAQCICAHQTkPEwxEPntEiRL176XeOEkj+8lZ7DJWWxuv7Zs2ULZ2dnEm5w0dsvt+kI/EHAKgYsXL9KTJ0+IN9HVSiQ+sYGuXbtGfGRa2MXF+yoMQ9KXCO5BAAQcRUBm/3x2pHrd+CgNL9lkVv/p0yfiN+HophtZ4uY3RBC/JpH4DTleed12I0thI0eOJD43kmbMmOE29aAPCIBAmBCAIRkmHQUxQSCSCYix1L1792rfmhNpfGRprX379pbfPhRpfKAvCICAPQRgSNrDGa2AAAiAAAiAAAiAgOsIwJB0XZdCIRAAARAAARAAARCwhwAMSXs4oxUQAAEQAAEQAAEQcB0BGJKu61IoBAIgAAIgAAIgAAL2EIAhaQ9ntAICIAACIAACIAACriMAQ9J1XQqFQAAEQAAEQAAEQMAeAjAk7eGMVkAABEAABEAABEDAdQRgSLquS6EQCIAACIAACIAACNhDAIakPZzRCgiAAAiAAAiAAAi4jgAMSdd1KRQCARAAARAAARAAAXsIwJC0hzNaAQEQAAEQAAEQAAHXEYAh6bouhUIgAAIgAAIgAAIgYA8BGJL2cEYrIAACIAACIAACIOA6An8AaESisS03zlQAAAAASUVORK5CYII="
    }
   },
   "cell_type": "markdown",
   "id": "e557668f",
   "metadata": {},
   "source": [
    "![image.png](attachment:image.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "a8b4b769",
   "metadata": {},
   "outputs": [],
   "source": [
    "A = np.random.normal(size=(4,4))\n",
    "B = np.random.normal(size=(4,4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "5ed1c7c8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.13693844,  2.50636533,  0.43546105,  0.89674835],\n",
       "       [ 0.48356421,  1.03004274,  0.1893715 ,  0.18130987],\n",
       "       [ 0.76428154,  0.08449883, -1.75441937,  2.07951077],\n",
       "       [ 0.01049086, -0.35706214, -0.3347277 , -0.51671177]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "C_gt = A @ B\n",
    "C_gt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "6733668f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0003799045189272077"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "C_dequant = quant_matmul(A, B).numpy()\n",
    "mse = np.mean((C_dequant - C_gt)**2)\n",
    "mse"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "07535226",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = torch.Tensor([[1,2],[3,4]])\n",
    "b = a * 3.5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "4962d0f2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[24.5000, 35.0000],\n",
       "        [52.5000, 77.0000]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a @ b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "39d63426",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[24.6282, 35.2782],\n",
       "        [52.3660, 76.9942]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "quant_matmul(a, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "9d56eb7a",
   "metadata": {},
   "outputs": [],
   "source": [
    "lin = torch.nn.Linear(2,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "4405e19f",
   "metadata": {},
   "outputs": [],
   "source": [
    "act = torch.Tensor([.1,.2])\n",
    "act.requires_grad = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "c80aa95d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([-0.6216,  0.1620, -0.2437], grad_fn=<AddBackward0>)"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lin(act)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "039b7db7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([-0.6212,  0.1619, -0.2437])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "quant_linear(lin, act)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "34049cf6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "trans-fat",
   "language": "python",
   "name": "trans-fat"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
